package com.kehao.leetcode.basic.linkedList;




public class LinkedListSolution4 {

    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode p;
        p = head;
        while (l1!=null&&l2!=null){
            if(l1.val<l2.val){
                p.next=l1;
                p = p.next;
                l1=l1.next;
            }else if (l1.val>l2.val){
                p.next=l2;
                p = p.next;
                l2=l2.next;
            }else{
                p.next=l1;
                p = p.next;
                l1=l1.next;
                p.next=l2;
                p = p.next;
                l2=l2.next;
            }
        }
        if(l1!=null){
            p.next=l1;
        }else {
            p.next=l2;
        }
        return head.next;
    }

}

